<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="整车物料编码:" prop="materialDataId">
            <el-select v-model="formModel.materialDataId" clearable filterable>
              <el-option
                v-for="item in carMaterialDataOptions"
                :key="item.materialDataId"
                :label="item.materialNo+'-'+item.materialDesc"
                :value="item.materialDataId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="能源种类:" prop="nyzl">
            <el-input v-model="formModel.nyzl" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最大净功率:" prop="zdjgl">
            <el-input v-model="formModel.zdjgl" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="驱动电机峰值功率:" prop="qddjfzgl">
            <el-input v-model="formModel.qddjfzgl" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="燃油消耗量限值:" prop="ryxhl">
            <el-input v-model="formModel.ryxhl" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="领跑值:" prop="lpz">
            <el-input v-model="formModel.lpz" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="驱动型式:" prop="qdxs">
            <el-input v-model="formModel.qdxs" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="其他信息:" prop="qtxx">
            <el-input v-model="formModel.qtxx" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="综合工况电能消耗量:" prop="zhgkdnxhl">
            <el-input v-model="formModel.zhgkdnxhl" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电能当量燃料消耗量:" prop="dndlrlxhl">
            <el-input v-model="formModel.dndlrlxhl" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="续航里程:" prop="xhlc">
            <el-input v-model="formModel.xhlc" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用日期:" prop="qyrq">
            <el-date-picker
              v-model="formModel.qyrq"
              align="right"
              type="date"
              placeholder="选择日期"
              :picker-options="pickerOptions"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备案号:" prop="bah">
            <el-input v-model="formModel.bah" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态:" prop="status">
            <hey-select v-model="formModel.status" clcd="is_effective_cd" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'FuelMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      // 日期组件
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      carMaterialDataOptions: [],
      formModel: {
        fuelId: '',
        materialDataId: '',
        nyzl: '',
        zdjgl: '',
        qddjfzgl: '',
        ryxhl: '',
        lpz: '',
        qdxs: '',
        qtxx: '',
        zhgkdnxhl: '',
        dndlrlxhl: '',
        xhlc: '',
        qyrq: '',
        bah: '',
        status: '1'
      },
      // 老数据
      oldFormModel: {
      },
      rules: {}
    }
  },
  computed: {},
  mounted() {
    this.init()
    this.getCarMaterialDataOptions()
  },
  methods: {
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.oldFormModel = JSON.parse(JSON.stringify(this.formModel))
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        materialDataId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        nyzl: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        qyrq: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        bah: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        status: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }]
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    // 获取整车物料编号列表
    getCarMaterialDataOptions() {
      const param = { materialTypeKey: 'car', status: '1' }
      this.get('/materialData/listMaterialData', param).then(res => {
        this.carMaterialDataOptions = res
      })
    },
    // 提交新增或修改
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.formModel.fuelId !== '') {
            // 判断是否修改
            if (JSON.stringify(this.formModel) !== JSON.stringify(this.oldFormModel)) {
              const tip = '修改成功！'
              const url = '/fuel/update'
              this.post(url, this.formModel).then(res => {
                this.$info(tip, () => {
                  this.$emit('mntSuc', res)
                  this.closeDialog()
                })
              })
            } else {
              this.$emit('mntSuc')
              this.closeDialog()
            }
          } else {
            const url = '/fuel/save'
            this.post(url, this.formModel).then(res => {
              const tip = '新增成功！'
              this.$info(tip, () => {
                this.$emit('mntSuc', res)
                this.closeDialog()
              })
            })
          }
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
